<template>
    <div>
        <el-button type="success" @click="isShowDialog('insert','literature')" icon="el-icon-plus" v-if="!disabled">新增</el-button>
        <el-table :data="pptList" :highlight-current-row="true" stripe border class="table-index"  style="width: 100%;">
            <el-table-column prop="fileName" label="附件名称" show-overflow-tooltip sortable min-width="100" align="center"></el-table-column>
            <el-table-column prop="desc" label="描述" show-overflow-tooltip	sortable min-width="100" align="center"></el-table-column>
            <el-table-column prop="uploaderName" label=" 上传人" show-overflow-tooltip sortable  min-width="90" align="center"></el-table-column>
            <el-table-column prop="uploadTime" label="上传时间" sortable min-width="120" align="center"></el-table-column>
            <el-table-column fixed="right" label="操作"  align="center" min-width="120">
                <template slot-scope="scope">
                    <el-button @click="literatureDownload(scope.row)" type="text" size="small">下载</el-button>
                    <el-button @click="deleteDate(scope.row)" type="text" size="small" v-if="!disabled"><span class="sub-fn-color3">删除</span></el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog :title="dialog.title" width="70%" top="40px"  center :visible.sync="dialog.isShow" :close-on-click-modal="false" :append-to-body='true'>
                <el-form :model="pptContent"   label-position="right" ref="literatureFrom" label-width="100px" :inline='true'>
                    <div el-row>
                        <el-form-item label="附件描述" prop="docName" >
                           <el-input type="textarea" :rows="2" placeholder="请输入附件描述"  v-model="pptContent.desc"></el-input>
                        </el-form-item>
                    </div>
                    <div el-row>
                        <el-form-item label="选择附件">
                            <el-upload action="/api/adscm/pebase/peAttachment/upload" class="upload-demo" style="width: 99%;margin: 0 auto;" ref="uploadEnclosure"
                                :auto-upload="false" :limit="1" :http-request="uploadSectionFile">
                                <el-button  slot="trigger" size="small" type="primary">选择附件</el-button>
                            </el-upload>
                        </el-form-item>
                    </div>
                    <el-form-item>
                        <el-button type="primary" @click="addUpdate()">新增</el-button>
                    </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>
<script>
import bloodKnowledgeApi from 'plugins/axios/bloodKnowledgeApi.js'
export default {
    props:{
        epKnowledId:{
            type:Number
        },
        disabled:Boolean
    },
    data(){
        return {
            tableHeight:'600px',
            pptList:[],/**原文献列表 */
            pagination:{
                pageSize:10,
                currentPage:1,
                total:0
            },
            pptContent:{},
            dialog:{
                title:'',
                isShow:false
            },


        }
    },
    watch:{
    },
    mounted(){
        this.getPptList();
    },
    methods:{
         getPptList(){/**获取文献列表 */
            let self = this;
            bloodKnowledgeApi.pePptList(self.epKnowledId).then(res=>{
                self.pptList=res.content;

            })
        },
        isShowDialog(){
            let self = this;
            self.dialog.isShow = true;
            self.dialog.title="新增附件"
             self.pptContent = {
                 desc:''
            }
        },
        addUpdate(){
            let self = this;
            this.$refs['literatureFrom'].validate((valid) => {
                if(valid){
                    if(self.$refs.uploadEnclosure.uploadFiles.length>0){
                        self.$refs.uploadEnclosure.submit();
                    }else{
                        self.$message.error(`请选择附件`);
                    }

                }
            })
        },
        uploadSectionFile(param){
            let self = this;
            let fileObj = param.file;
            let form =new FormData();
            let peDocument={
                did:self.epKnowledId,
                desc:self.pptContent.desc
            }
            form.append('peAttachment',JSON.stringify(peDocument));
            form.append('file',fileObj);
            bloodKnowledgeApi.pePptUpload(form).then(res=>{
                self.$message.success(`附件新增成功`);
                self.dialog.isShow=false;
                self.$refs.uploadEnclosure.clearFiles();
                self.getPptList();
            },error=>{
                self.$emit('addHandle',self.activeName,true);
            })
        },
        literatureDownload(row){/**下载原文献 */
            window.location.href = bloodKnowledgeApi.pptDowload(row.id);
        },
        deleteDate(row){/**删除原文献 */
            let self = this;
            self.$confirm('确认是否删除?', '提示', {
                confirmButtonClass: 'el-button--warning'
                }).then(() => {
                    bloodKnowledgeApi.deletePePpt(row.id).then(res=>{
                        self.getPptList();
                        self.$message.success("附件删除成功！");
                    })
                }).catch(() => {
            });
        }
    }
}
</script>
<style scoped>

</style>
